<template>
  <div style="widht: 500px" class="chart" id="piechart"></div>
</template>

<script>
import * as echarts from 'echarts'
import {commlistAPI,messlistAPI,articlelistAPI,userlistAPI} from '../../api'
export default {
  data(){
    return {
      list:[]
    }
  },
  watch:{
  },
  mounted () {
    this.getData()
  },
  methods: {
    init () {
      var chartDom = document.getElementById('piechart')
      var myChart = echarts.init(chartDom)
      var option

      option = {
        tooltip: {
          trigger: 'item'
        },
        legend: {
          top: '5%',
          left: 'center'
        },
        series: [
          {
            name: 'Access From',
            type: 'pie',
            radius: ['40%', '70%'],
            avoidLabelOverlap: false,
            itemStyle: {
              borderRadius: 10,
              borderColor: '#fff',
              borderWidth: 2
            },
            label: {
              show: false,
              position: 'center'
            },
            emphasis: {
              label: {
                show: true,
                fontSize: 40,
                fontWeight: 'bold'
              }
            },
            labelLine: {
              show: false
            },
            data:this.list
          }
        ]
      }

      option && myChart.setOption(option)
    },
    async getData(){
      let res =await commlistAPI()
      this.list.push({value:res.data.data.length,name:'评论'})
       res =await messlistAPI()
      this.list.push({value:res.data.data.length,name:'留言'})
      res =await articlelistAPI()
      this.list.push({value:res.data.data.length,name:'文章'})
      res =await userlistAPI()
      this.list.push({value:res.data.data.length,name:'管理员'})
      const length = this.list.reduce((prev,item)=>prev+=item.value,0)
      this.list.unshift({value:length,name:'全部'})
      this.$nextTick(()=>{
        this.init()
      })
    }
  }
};
</script>

<style scoped>
.chart {
  width: 40vw;
  height: 50vh;
}
</style>